package com.how2java.tmall.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.how2java.tmall.mapper.OrderitemMapper;
import com.how2java.tmall.mapper.ProductMapper;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.pojo.Orderitem;
import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.pojo.Productimage;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.service.ProductImageService;
import com.how2java.tmall.service.ProductService;
import com.how2java.tmall.service.ReviewService;

@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductMapper dao;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ProductImageService productImageService;
	@Autowired
	private OrderitemMapper orderItemMapper;
	@Autowired
	private ReviewService reviewService;

	@Override
	public void add(Product p) {
		dao.insertSelective(p);
	}

	@Override
	public void delete(int id) {
		dao.deleteByPrimaryKey(id);
	}

	@Override
	public void update(Product p) {
		dao.updateByPrimaryKeySelective(p);
	}

	@Override
	public Product get(int id) {
		Product p = dao.selectByPrimaryKey(id);
		setCategory(p);
		setFirstProductImage(p);
		return p;
	}

	@Override
	public List<Product> list(int cid) {
		List<Product> ps = dao.getByCid(cid);
		setCategory(ps);
		setFirstProductImage(ps);
		return ps;
	}
	
	public void setCategory(List<Product> ps){
        for (Product p : ps) {
            setCategory(p);
        }
    }
    public void setCategory(Product p){
        int cid = p.getCid();
        Category c = categoryService.get(cid);
        p.setCategory(c);
    }

    
	@Override
	public void setFirstProductImage(Product p) {
		List<Productimage> pis = productImageService.list(p.getId(), ProductImageService.type_single);
        if (!pis.isEmpty()) {
        	Productimage pi = pis.get(0);
            p.setFirstProductImage(pi);
        }
	}
	
	
	public void setFirstProductImage(List<Product> ps) {
        for (Product p : ps) {
            setFirstProductImage(p);
        }
    }

	@Override
	public void fill(List<Category> cs) {
		for (Category c : cs) {
            fill(c);
        }
	}

	@Override
	public void fill(Category c) {
		List<Product> ps = list(c.getId());
        c.setProducts(ps);
	}

	@Override
	public void fillByRow(List<Category> cs) {
		int productNumberEachRow = 8;
        for (Category c : cs) {
            List<Product> products =  c.getProducts();
            List<List<Product>> productsByRow =  new ArrayList<>();
            for (int i = 0; i < products.size(); i+=productNumberEachRow) {
                int size = i+productNumberEachRow;
                size= size>products.size()?products.size():size;
                List<Product> productsOfEachRow =products.subList(i, size);
                productsByRow.add(productsOfEachRow);
            }
            c.setProductsByRow(productsByRow);
        }
	}

	@Override
	public int getSaleCount(int pid) {
		
		List<Orderitem> ois =orderItemMapper.getOrderItemListByPid(pid);
		int result =0;
        for (Orderitem oi : ois) {
            result+=oi.getNumber();
        }
        return result;
		
		
	}

	@Override
	public void setSaleAndReviewNumber(Product p) {
		p.setSaleCount(getSaleCount(p.getId()));
		p.setReviewCount(reviewService.getCount(p.getId()));
		
	}

	@Override
	public void setSaleAndReviewNumber(List<Product> ps) {
		for(Product p : ps) {
			setSaleAndReviewNumber(p);
		}
	}

	@Override
	public List<Product> search(String keyword) {
        List<Product> result = dao.selectByName(keyword);
        setFirstProductImage(result);
        setCategory(result);
        return result;
	}
}
